﻿using Microsoft.Practices.Unity;
using RockBus.ConfigurationService.Interfaces;
using RockBus.Hosting;
using RockBus.Hosting.Interfaces;
using RockBus.Hosting.Web;
using RockBus.Initialization.Interfaces;
using RockBus.MonitoringService.Interfaces;
using RockBus.Routing;
using RockBus.Routing.Interfaces;
using RockBus.Routing.SendPorts;
using RockBus.Services.WebApi;
using RockBus.Transport.Db;

namespace RockBus.Composition
{
    public class DbIisDependencyManager : DependencyManagerBase
    {
        protected override void FillContainer(IUnityContainer container)
        {
            this.FillContainerWithSqlDbSubscriptionRespository(container);
            this.FillContainerWithSqlDbMessageRepository(container);
            this.FillContainerWithWebApiDependencies(container);

            container
            .RegisterType<IMessageRepositoryHelper, MessageRepositoryHelper>(new ContainerControlledLifetimeManager())
            .RegisterType<IMessageProcessor, DbMessageProcessor>(InstanceNames.PersistenceSendPort, new ContainerControlledLifetimeManager())
            .RegisterType<IMessageProcessor, DeliverySendPort>(InstanceNames.DeliverySendPort, new ContainerControlledLifetimeManager())
            .RegisterType<IMessageProcessor, DbErrorProcessor>(InstanceNames.ErrorManager, new ContainerControlledLifetimeManager())

            // PublisherManager dependencies
            .RegisterType<IMessagePublisher, DbMessagePublisher>(InstanceNames.DbMessagePublisher, new ContainerControlledLifetimeManager())
            .RegisterType<IPublisherManager, PublisherManager>(new ContainerControlledLifetimeManager())
            .RegisterType<IInitialization, PublisherManagerInitialization>(InstanceNames.PublisherManagerInitialization, new ContainerControlledLifetimeManager())
            ;
        }

        protected override void FillContainerWithHostingDependencies(IUnityContainer container)
        {
            container
            .RegisterType<IServiceHostManager, DynamicServiceHostManager>(new ContainerControlledLifetimeManager())
            .RegisterType<IReceivePortServiceHostFactory, WebReceivePortServiceHostFactory>(new ContainerControlledLifetimeManager())
            .RegisterType<IInitialization, ServiceHostManagerInitialization>(InstanceNames.ServiceHostManagerInitialization, new ContainerControlledLifetimeManager());
        }
    }
}